package com.hightrast.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hightrast.service.api.baseEntity.exception.ServiceException;
import com.hightrast.service.api.domain.qo.rastUserBank.SaveUpdateUserBankQo;
import com.hightrast.service.api.domain.qo.rastVendor.AddUserVendorQo;
import com.hightrast.service.api.entity.RastUser;
import com.hightrast.service.api.entity.RastUserBank;
import com.hightrast.service.mapper.RastUserBankMapper;
import com.hightrast.service.service.IRastUserBankService;
import com.hightrast.service.service.IRastUserService;
import com.hightrast.service.util.BaseEntityUtil;
import com.hightrast.service.util.SecureUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 用户账号-银行账号关联表 服务实现类
 * </p>
 *
 * @author hx
 * @since 2025-01-11
 */
@Service
public class RastUserBankServiceImpl extends ServiceImpl<RastUserBankMapper, RastUserBank> implements IRastUserBankService {

    @Resource
    @Lazy
    IRastUserService userService;

    @Resource
    RastUserVendorServiceImpl rastUserVendorService;

    /**
     * 获取用户所存的银行账号
     *
     * @param userId
     * @return
     */
    @Override
    public List<RastUserBank> getUserBank(Long userId) {
        return list(new LambdaQueryWrapper<RastUserBank>().eq(RastUserBank::getUserId, userId));
    }

    /**
     * 新增、修改用户支付银行信息、品牌信息、用户信息
     *
     * @param qo
     */
    @Override
    @Transactional
    public void saveUpdateUserBank(SaveUpdateUserBankQo qo) {
        RastUser user = SecureUtil.getUser();
        RastUserBank userBank = this.getOne(new LambdaQueryWrapper<RastUserBank>().eq(RastUserBank::getUserId, qo.getUserId()));
        // 更新头像信息
        try {
            RastUser editUser = userService.getById(qo.getUserId());
//            if (StringUtils.isNotBlank(qo.getEmail())) {
//                editUser.setEmail(qo.getEmail());
//            }
            editUser.setUserName(qo.getUserName());
            editUser.setAvatar(qo.getAvatarUrl());
            userService.updateById(editUser);
        } catch (Exception e) {
            e.printStackTrace();
            // 头像上传失败,请联系管理员
            throw new ServiceException("Avatar upload failed, please contact administrator");
        }
        if (Objects.nonNull(userBank)) {
            // 更新支付银行
            BeanUtil.copyProperties(qo, userBank);
            BaseEntityUtil.update(userBank, user);
            this.updateById(userBank);
        } else {
            // 新增支付银行信息
            RastUserBank addUserBank = BeanUtil.copyProperties(qo, RastUserBank.class);
            BaseEntityUtil.save(addUserBank, user);
            this.save(addUserBank);
        }
        // 品牌不为空时更新品牌信息
        if (StringUtils.isNotBlank(qo.getVendorId())) {
            AddUserVendorQo userVendorQo = new AddUserVendorQo();
            userVendorQo.setVendor(qo.getVendor());
            userVendorQo.setUserId(qo.getUserId());
            userVendorQo.setVendorId(Long.valueOf(qo.getVendorId()));
            userVendorQo.setRemark(qo.getRemark());
            rastUserVendorService.saveUpdateVendorUser(userVendorQo);
        }
    }
}
